/**   
 * @Title: MyJob.java 
 * @Package com.cyber.quartz 
 * @Description: TODO(用一句话描述该文件做什么) 
 * @author cssuger@163.com   
 * @date 2016年12月16日 上午10:57:01 
 * @version V1.0   
 */
package cn.com.ytst.data.integration.manager.job;

import java.util.regex.Pattern;


import org.quartz.DisallowConcurrentExecution;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.StatefulJob;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import com.google.common.base.Strings;
import com.google.common.util.concurrent.ListeningExecutorService;
import com.ytst.framework.common.tools.DateUtil;
import com.ytst.framework.common.tools.JsonUtil;

import cn.com.ytst.data.integration.entity.TaskLog;
import cn.com.ytst.data.integration.util.SpringContextUtil;



/**
 * @ClassName: MyJob
 * @Description: 1这这里主要涉及2大类型的 一种是通信类型的，
 * 一种是class类型的，当前主要以class类型的 2
 *               考虑到当前使用的是spring工程
 * @author cssuger@163.com
 * @date 2016年12月16日 上午10:57:01
 */
@SuppressWarnings("deprecation")
@DisallowConcurrentExecution
public class QuartzJobFactoryDisallowConcurrentExecution implements StatefulJob {

	private Logger logger = LoggerFactory.getLogger(QuartzJobFactoryDisallowConcurrentExecution.class);

	private final String regExpress = "^([hH][tT]{2}[pP]://|[hH][tT]{2}[pP][sS]://)(([A-Za-z0-9-~]+).)+([A-Za-z0-9-~\\/])+$";
	
	private ListeningExecutorService listeningExecutorService = SpringContextUtil.getBean("listeningExecutorService", ListeningExecutorService.class);
	
	//private TaskLogService taskLogService = SpringContextUtil.getBean("taskLogService", TaskLogService.class);

	private ThreadPoolTaskExecutor threadpooltaskexecutor = SpringContextUtil.getBean("threadpooltaskexecutor", ThreadPoolTaskExecutor.class);

	private Pattern p = Pattern.compile(regExpress, Pattern.CASE_INSENSITIVE);

	@Override
	public void execute(JobExecutionContext context) throws JobExecutionException {
		TaskLog tasklog = new TaskLog();
		logger.info("开始时间:{}", DateUtil.now("yyyy-MM-dd HH:mm:ss"));
		String jsonText = (String) context.getJobDetail().getJobDataMap().get("task");
		String prarms = (String) context.getJobDetail().getJobDataMap().get("prarms");
		Object object = context.getJobDetail().getJobDataMap().get("object");
		TaskManager taskmanager = (TaskManager) JsonUtil.jsonToObject(jsonText, TaskManager.class);

		if (!Strings.isNullOrEmpty(jsonText)) {

			if (taskmanager.jobName.startsWith("http://")) {
				// 说明是http
				// 服务，想服务器端发生请求，服务器端最好是异步的，到时候建立一张表去监控每个作业的状态，目的是防止该作业还没有完成，在下个时刻又开始改作业了。
				// 记住一点要记录日志

			} else {
				//String prarms,Object object,TaskManager taskmanager,TaskLog tasklog,ListeningExecutorService listeningExecutorService,TaskLogService taskLogService
				ScheduleRunnable scheduleRunnable = new ScheduleRunnable(prarms,object,taskmanager,tasklog,listeningExecutorService);
				threadpooltaskexecutor.submit(scheduleRunnable);
			}

		}

	}





}
